﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;

namespace WorkFlow.Data
{
    /// <summary>
    /// 将DataRow/DataTable转换成Entity类型
    /// </summary>
    public class CommentTool
    {

        public bool ShowNav = true;
        private static Dictionary<string, int> OprationStrToIntDict = null;
        private static Dictionary<int, string> OprationIntToStrDict = null;
        /// <summary>
        /// 将DataTable转换成Entity列表
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List<T> DataTableToEntityArray<T>(DataTable dataSource) where T : new()
        {
            if (dataSource == null || dataSource.Rows.Count == 0)
            {
                return new List<T>();
            }
            List<T> list = new List<T>();
            foreach (DataRow dr in dataSource.Rows)
            {
                T model = new T();
                for (int i = 0; i < dr.Table.Columns.Count; i++)
                {
                    PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                    if (propertyInfo != null && dr[i] != DBNull.Value)
                    {
                        switch (propertyInfo.PropertyType.Name)
                        {
                            case "bool":
                            case "Boolean":
                                propertyInfo.SetValue(model, (dr[i].ToString() == "1" || dr[i].ToString() == "true"), null);
                                break;
                            case "Int":
                            case "Int32":
                                propertyInfo.SetValue(model, int.Parse(dr[i].ToString()), null);
                                break;
                            case "String":
                                propertyInfo.SetValue(model, dr[i].ToString(), null);
                                break;
                            default:
                                propertyInfo.SetValue(model, dr[i].ToString(), null);
                                break;
                        }
                    }
                }
                list.Add(model);
            }
            return list;
        }

        public static int GetOprationIntType(string opration)
        {
            if (OprationStrToIntDict == null)
            {
                OprationStrToIntDict = new Dictionary<string, int>();
                OprationStrToIntDict[""] = 0;
                OprationStrToIntDict["&"] = 1;
                OprationStrToIntDict["|"] = 2;
                OprationStrToIntDict["!"] = 3;
                OprationStrToIntDict["="] = 4;
                OprationStrToIntDict["!="] = 5;
                OprationStrToIntDict[">"] = 6;
                OprationStrToIntDict[">="] = 7;
                OprationStrToIntDict["<"] = 8;
                OprationStrToIntDict["<="] = 9;
            }
            return OprationStrToIntDict[opration];
        }

        public static string GetOprationStrType(int opration)
        {
            if (OprationIntToStrDict == null)
            {
                OprationIntToStrDict = new Dictionary<int, string>();
                OprationIntToStrDict[0] = "";
                OprationIntToStrDict[1] = "&";
                OprationIntToStrDict[2] = "|";
                OprationIntToStrDict[3] = "!";
                OprationIntToStrDict[4] = "=";
                OprationIntToStrDict[5] = "!=";
                OprationIntToStrDict[6] = ">";
                OprationIntToStrDict[7] = ">=";
                OprationIntToStrDict[8] = "<";
                OprationIntToStrDict[9] = "<=";
            }
            return OprationIntToStrDict[opration];
        }
        public static string SerializeJson<T>(T obj, int indentation = 0, char indentchar = ' ')
        {
            StringWriter writer = new StringWriter();
            JsonTextWriter jsoonWriter = new JsonTextWriter(writer)
            {
                Formatting = Formatting.Indented,
                Indentation = indentation,
                IndentChar = indentchar
            };
            JsonSerializer serializer = new JsonSerializer();
            serializer.Serialize(jsoonWriter, obj);
            return writer.ToString();
        }
        public static T DeserializeJson<T>(string json)
        {
            return JsonConvert.DeserializeObject<T>(json);
        }
    }
}
